package clustering

import de.lmu.ifi.dbs.elki.distance.DoubleDistance
import de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.{FileParameter, Parameter}
import java.io.File
import de.lmu.ifi.dbs.elki.normalization.DummyNormalization
import de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization.{ListParameterization, Parameterization}
import collection.JavaConversions
import de.lmu.ifi.dbs.elki.data.model.Model
import de.lmu.ifi.dbs.elki.database.connection.{AbstractDatabaseConnection, FileBasedDatabaseConnection}
import de.lmu.ifi.dbs.elki.utilities.pairs.Pair
import de.lmu.ifi.dbs.elki.data.{DoubleVector, DatabaseObject, Clustering}

import JavaConversions._
import de.lmu.ifi.dbs.elki.algorithm.clustering.{DeLiClu, DBSCAN}
import de.lmu.ifi.dbs.elki.result.ClusterOrderResult
import de.lmu.ifi.dbs.elki.database.{SpatialIndexDatabase, Associations, SequentialDatabase, Database}
import de.lmu.ifi.dbs.elki.index.tree.spatial.rstarvariants.deliclu.{DeLiCluTree, DeLiCluNode, DeLiCluEntry}

/**
 * Created by IntelliJ IDEA.
 * User: denny
 * Date: Jul 26, 2010
 * Time: 1:09:50 PM
 * To change this template use File | Settings | File Templates.
 */

object ElkiTest1 extends Application{
  val parm = new ListParameterization(JavaConversions.asCollection(List()))
  //parm.addParameter(FileBasedDatabaseConnection.INPUT_ID, new File("/opt/elki/exampledata.txt"))

//  val fileParm =  new FileParameter(FileBasedDatabaseConnection.INPUT_ID, FileParameter.FileType.INPUT_FILE )
//  fileParm.setValue(new File("/opt/elki/exampledata.txt"))
//  parm.setValueForOption(fileParm)

//  class MySeqDB extends SequentialDatabase{
//    override def insert(objectsAndAssociationsList: java.util.List[Pair[Nothing, Associations]]) = {
//      super.insert(objectsAndAssociationsList)
//    }
//
//  }
//
//  parm.addParameter(AbstractDatabaseConnection.DATABASE_ID, classOf[MySeqDB])
//  val dbc = new FileBasedDatabaseConnection(parm)
//
//  val db = dbc.getDatabase(new DummyNormalization)
//  println("db size %d".format( db.size ))

  //val dbseq = new SequentialDatabase[DoubleVector]

  parm.addParameter(SpatialIndexDatabase.INDEX_ID, classOf[DeLiCluTree[DoubleVector]])
  val dbseq = new SpatialIndexDatabase[DoubleVector,DeLiCluNode,DeLiCluEntry](parm)
  //dbseq.insert()
  for( i <- 1 to 10 ){
    val vec = new DoubleVector(Array(10.0,i))
    dbseq.insert(new Pair(vec, new Associations()))
  }
  for( i <- 50 to 8000 ){
    val vec = new DoubleVector(Array(10.0,i))
    dbseq.insert(new Pair(vec, new Associations()))
  }
  println("dbseq size %d".format( dbseq.size ))

  //parm.addParameter(DBSCAN.MINPTS_ID, new java.lang.Integer(3))

//  parm.addParameter(DBSCAN.EPSILON_ID, new java.lang.Double(5))
//  val alg = new DBSCAN[DoubleVector,DoubleDistance](parm) //  [DoubleVector, DoubleDistance](parm)
//  val cl :Clustering[Model] = alg.run(dbseq)
//  println("clusters count %d".format( cl.getAllClusters.size ))
//  for( cli <- cl.getAllClusters ){
//    val ch = cli.getChildren
//    val ids = cli.getIDs
//    println("ch=%s, ids=%s, vals=%s".format(ch,ids, ids.map(i => dbseq.get(i)) ))
//  }

  parm.addParameter(DeLiClu.MINPTS_ID, new java.lang.Integer(3))
  val alg = new DeLiClu[DoubleVector,DoubleDistance](parm)
  val res : ClusterOrderResult[DoubleDistance] = alg.run(dbseq)
  for( orde <- res.getClusterOrder ){
    println("%s reachability %s".format( dbseq.get(orde.getID), orde.getReachability ))
  }


//  val d:Database = new SequentialDatabase[DoubleVector]
//  d.
//  alg.run()

}
